package com.vma.jifu.backsys.controller;

import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.vma.tools.GetPathByDate;
import com.vma.tools.Qiniu;
import com.vma.tools.ReturnsUtil;
import org.apache.commons.collections.map.HashedMap;

import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by kitabi on 2017/6/25.
 */
public class FileController extends  BaseController{

    //要上传的空间
    String bucketname = "vma-back";

    String filename;

    /**
     * upload 图片上传方法
     * @throws IOException
     */
    public void upload() {
        try{
            UploadFile file = getFile();
            String imgName = file.getFileName();

            File source = new File(PathKit.getWebRootPath() + "/temp/" + imgName); // 获取临时文件对象
//            String savePath = PathKit.getWebRootPath() + "/" + GetPathByDate.getPath();

            String savePath =
//                    "/alidata01/chenz/www/Static_war_exploded" +
                            "/" + GetPathByDate.getPath();
            FileInputStream fis = new FileInputStream(source);
//
            File targetDir = new File(savePath);
            if (!targetDir.exists()) {
                targetDir.mkdirs();
            }

            File target = new File(targetDir, imgName);
            if (!target.exists()) {
                target.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(target);
            byte[] bts = new byte[1024 * 20];
            while (fis.read(bts, 0, 1024 * 20) != -1) {
                fos.write(bts, 0, 1024 * 20);
            }

            fos.close();
            fis.close();
            renderJson(ReturnsUtil.returnSuccess( "/"+GetPathByDate.getPath()+"/"+imgName));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e ));

        }

    }


    public void uploadImg() throws Exception {
        UploadFile uploadFile = getFile("uploadFile", PathKit.getWebRootPath()
                + "/temp", 20 * 1024 * 1024, "utf-8"); // 最大上传20M的图片
        // 异步上传时，无法通过uploadFile.getFileName()获取文件名
        String fileName = getPara("fileName");
        fileName = fileName.substring(fileName.lastIndexOf("\\") + 1); // 去掉路径

        // 异步上传时，无法通过File source = uploadFile.getFile();获取文件
        File source = new File(PathKit.getWebRootPath() + "/temp/" + fileName); // 获取临时文件对象

        String extension = fileName.substring(fileName.lastIndexOf("."));
        String savePath = PathKit.getWebRootPath() + "/upload/images/"
                + GetPathByDate.getPath();
        FileInputStream fis = new FileInputStream(source);

        File targetDir = new File(savePath);
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }

        File target = new File(targetDir, fileName);
        if (!target.exists()) {
            target.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(target);
        byte[] bts = new byte[1024 * 20];
        while (fis.read(bts, 0, 1024 * 20) != -1) {
            fos.write(bts, 0, 1024 * 20);
        }

        fos.close();
        fis.close();
    }

    public void token(){

        Qiniu qiniu= Qiniu.create("ORP1KHjrSqmQGUqbjGsVIDIZT3YWSg92TldKWvOd","mVcJBBS7D8dJgedIuNLzkNygjQZLj_CcvWsBs79Z");

        filename="z"+String.valueOf( (new Date()).getTime());
        String token = qiniu.uploadToken(bucketname);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("uptoken",token);
        renderJson(map);
    }
}
